package com.project.homework4_8.dao.impl;

import java.sql.*;

public class BaseDao {
    private Connection con;
    private PreparedStatement ps;
    private ResultSet rs;

    /**
     * 创建并返回连接
     * @return 连接对象
     */
    public void getConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:12345/db?characterEncoding=utf-8",
                    "root","lovo");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void closeConnection(){
        try {
            if (rs!=null) {
                rs.close();
            }
            if (ps!=null) {
                ps.close();
            }
            if (con!=null) {
                con.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void addLog(String sql,Object... valueArray){
        try {
            ps = con.prepareStatement(sql);
            for (int i=0;i<valueArray.length;i++){
                ps.setObject(i+1,valueArray[i]);
            }
            ps.executeUpdate();
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }
}
